package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Region;
import cn.stylefeng.guns.sys.modular.tao.mapper.RegionMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.RegionParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.RegionResult;
import  cn.stylefeng.guns.sys.modular.tao.service.RegionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 行政区域划分等级 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-24
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    @Autowired
    RegionMapper regionMapper;
    @Override
    public void add(RegionParam param){
        Region entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(RegionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(RegionParam param){
        Region oldEntity = getOldEntity(param);
        Region newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public RegionResult findBySpec(RegionParam param){
        return null;
    }

    @Override
    public List<RegionResult> findListBySpec(RegionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(RegionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Region> listsByS() {
        return regionMapper.listsByS();
    }

    @Override
    public List<Region> listsByP(String parentCode) {
       return regionMapper.listsByP(parentCode);
    }

    @Override
    public Region getAreaNameByCode(String area) {
        return regionMapper.getAreaNameByCode(area);
    }


    @Override
    public List<Region> listsByAllCity(String parentCode) {
        return regionMapper.listsByAllCity(parentCode);
    }

    @Override
    public List<Region> listsBySAll() {
        return regionMapper.listsBySAll();
    }

    private Serializable getKey(RegionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Region getOldEntity(RegionParam param) {
        return this.getById(getKey(param));
    }

    private Region getEntity(RegionParam param) {
        Region entity = new Region();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
